package com.woniuxy.house.web;

import com.woniuxy.house.entity.*;
import com.woniuxy.house.utils.JwtUtil;
import com.woniuxy.house.vo.AddSourceVo;
import com.woniuxy.house.vo.ShaiXuan;
import com.woniuxy.house.service.SourceService;
import com.woniuxy.house.vo.ResponseEntity;
import com.woniuxy.house.vo.UserAndSourceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/source")
public class SourceController {
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private SourceService sourceService;

    /**
     * 获取整租房源（该城市）
     * 第三个参数
     */
    @RequestMapping("/getZhengZu")

    public ResponseEntity getZhengZu(String city, Integer page, ShaiXuan shaiXuan) {
        if (city == null || city.equals("")) {
            return ResponseEntity.NO_VALID;
        }
        if (page == null) {
            return ResponseEntity.NO_VALID;
        }
        Source source = new Source();
        source.setRentalHouseWay("z");


        //判断是否有价格区间筛选条件
        if (shaiXuan.getPrice() != null && !shaiXuan.getPrice().equals("无") && !shaiXuan.getPrice().equals("不限")) {
            if (shaiXuan.getPrice().indexOf(">") != -1) {
                String startPrice = shaiXuan.getPrice().substring(1);
                source.setStartPrice(new BigDecimal(startPrice));
            } else {
                String[] prices = shaiXuan.getPrice().split("-");
                source.setStartPrice(new BigDecimal(prices[0]));
                source.setEndPrice(new BigDecimal(prices[1]));
            }

        }
        //判断是否有方向的筛选条件
        if (shaiXuan.getTowards() != null && !shaiXuan.getTowards().equals("无")) {
            source.setTowards(shaiXuan.getTowards());

        }
        //判断是否有面积区间的判断条件
        if (shaiXuan.getSize() != null && !shaiXuan.getSize().equals("无")) {
            if (shaiXuan.getSize().indexOf("-") != -1) {
                String[] sizes = shaiXuan.getSize().split("-");
                source.setStartSize(Double.parseDouble(sizes[0]));
                source.setEndSize(Double.parseDouble(sizes[1]));
            }
            //判断是否为10以下
            if (shaiXuan.getSize().indexOf("以下") != -1) {
                String endSize = shaiXuan.getSize().substring(0, 2);
                source.setEndSize(Double.parseDouble(endSize));
            } else {
                String startSize = shaiXuan.getSize().substring(0, 2);
                source.setStartSize(Double.parseDouble(startSize));
            }
        }
        //判断是否居室的筛选条件
        if (shaiXuan.getRoom() != null && !shaiXuan.getRoom().equals("无")) {
            if (shaiXuan.getRoom().indexOf("以上") != -1) {
                String startJuShi = shaiXuan.getRoom().substring(0, 1);
                source.setStartJuShi(Integer.parseInt(startJuShi));

            } else {
                String startJuShi = shaiXuan.getRoom().substring(0, 1);
                source.setJuShi(Integer.parseInt(startJuShi));
            }
        }
        //判断是否有搜索内容
        if (shaiXuan.getSearchText() != null && !shaiXuan.getSearchText().equals("无")) {
            source.setTitle(shaiXuan.getSearchText());
        }

        source.setCity(city);
        PageBean<Source> pageBean = sourceService.getPageByCondition(source, page, shaiXuan.getSort());
        if (pageBean.getData().size() == 0) {

            return new ResponseEntity<>("404", "未找到相关房源", null);
        }
        return new ResponseEntity("200", "ok", pageBean);

    }

    /**
     * 获取合租房源（该城市）
     */
    @RequestMapping("/getHeZu")
    public ResponseEntity getHeZu(String city, Integer page, ShaiXuan shaiXuan) {
        if (city == null || city.equals("")) {
            return ResponseEntity.NO_VALID;
        }
        if (page == null) {
            return ResponseEntity.NO_VALID;
        }
        Source source = new Source();
        source.setRentalHouseWay("h");

        //判断是否有价格区间筛选条件
        if (shaiXuan.getPrice() != null && !shaiXuan.getPrice().equals("无") && !shaiXuan.getPrice().equals("不限")) {
            if (shaiXuan.getPrice().indexOf(">") != -1) {
                String startPrice = shaiXuan.getPrice().substring(1);
                source.setStartPrice(new BigDecimal(startPrice));
            } else {
                String[] prices = shaiXuan.getPrice().split("-");
                source.setStartPrice(new BigDecimal(prices[0]));
                source.setEndPrice(new BigDecimal(prices[1]));
            }

        }
        //判断是否有方向的筛选条件
        if (shaiXuan.getTowards() != null && !shaiXuan.getTowards().equals("无")) {
            source.setTowards(shaiXuan.getTowards());

        }
        //判断是否有面积区间的判断条件
        if (shaiXuan.getSize() != null && !shaiXuan.getSize().equals("无")) {
            if (shaiXuan.getSize().indexOf("-") != -1) {
                String[] sizes = shaiXuan.getSize().split("-");
                source.setStartSize(Double.parseDouble(sizes[0]));
                source.setEndSize(Double.parseDouble(sizes[1]));
            }
            //判断是否为10以下
            if (shaiXuan.getSize().indexOf("以下") != -1) {
                String endSize = shaiXuan.getSize().substring(0, 2);
                source.setEndSize(Double.parseDouble(endSize));
            } else {
                String startSize = shaiXuan.getSize().substring(0, 2);
                source.setStartSize(Double.parseDouble(startSize));
            }
        }
        //判断是否居室的筛选条件
        if (shaiXuan.getRoom() != null && !shaiXuan.getRoom().equals("无")) {
            if (shaiXuan.getRoom().indexOf("以上") != -1) {
                String startJuShi = shaiXuan.getRoom().substring(0, 1);
                source.setStartJuShi(Integer.parseInt(startJuShi));

            } else {
                String startJuShi = shaiXuan.getRoom().substring(0, 1);
                source.setJuShi(Integer.parseInt(startJuShi));
            }
        }
        //判断是否有搜索内容
        if (shaiXuan.getSearchText() != null && !shaiXuan.getSearchText().equals("无")) {
            source.setTitle(shaiXuan.getSearchText());
        }

        source.setCity(city);
        PageBean<Source> pageBean = sourceService.getPageByCondition(source, page, shaiXuan.getSort());
        if (pageBean.getData().size() == 0) {

            return new ResponseEntity<>("404", "未找到相关房源", null);
        }
        return new ResponseEntity("200", "ok", pageBean);


    }

    @RequestMapping("/getYueZu")
    public ResponseEntity getYueZu(String city, Integer page, ShaiXuan shaiXuan) {
        if (city == null || city.equals("")) {
            return ResponseEntity.NO_VALID;
        }
        if (page == null) {
            return ResponseEntity.NO_VALID;
        }
        Source source = new Source();
        source.setRentalTimeWay("月租");

        //判断是否有价格区间筛选条件
        if (shaiXuan.getPrice() != null && !shaiXuan.getPrice().equals("无") && !shaiXuan.getPrice().equals("不限")) {
            if (shaiXuan.getPrice().indexOf(">") != -1) {
                String startPrice = shaiXuan.getPrice().substring(1);
                source.setStartPrice(new BigDecimal(startPrice));
            } else {
                String[] prices = shaiXuan.getPrice().split("-");
                source.setStartPrice(new BigDecimal(prices[0]));
                source.setEndPrice(new BigDecimal(prices[1]));
            }

        }
        //判断是否有方向的筛选条件
        if (shaiXuan.getTowards() != null && !shaiXuan.getTowards().equals("无")) {
            source.setTowards(shaiXuan.getTowards());

        }
        //判断是否有面积区间的判断条件
        if (shaiXuan.getSize() != null && !shaiXuan.getSize().equals("无")) {
            if (shaiXuan.getSize().indexOf("-") != -1) {
                String[] sizes = shaiXuan.getSize().split("-");
                source.setStartSize(Double.parseDouble(sizes[0]));
                source.setEndSize(Double.parseDouble(sizes[1]));
            }
            //判断是否为10以下
            if (shaiXuan.getSize().indexOf("以下") != -1) {
                String endSize = shaiXuan.getSize().substring(0, 2);
                source.setEndSize(Double.parseDouble(endSize));
            } else {
                String startSize = shaiXuan.getSize().substring(0, 2);
                source.setStartSize(Double.parseDouble(startSize));
            }
        }
        //判断是否居室的筛选条件
        if (shaiXuan.getRoom() != null && !shaiXuan.getRoom().equals("无")) {
            if (shaiXuan.getRoom().indexOf("以上") != -1) {
                String startJuShi = shaiXuan.getRoom().substring(0, 1);
                source.setStartJuShi(Integer.parseInt(startJuShi));

            } else {
                String startJuShi = shaiXuan.getRoom().substring(0, 1);
                source.setJuShi(Integer.parseInt(startJuShi));
            }
        }
        //判断是否有搜索内容
        if (shaiXuan.getSearchText() != null && !shaiXuan.getSearchText().equals("无")) {
            source.setTitle(shaiXuan.getSearchText());
        }

        source.setCity(city);
        PageBean<Source> pageBean = sourceService.getPageByCondition(source, page, shaiXuan.getSort());
        if (pageBean.getData().size() == 0) {

            return new ResponseEntity<>("404", "未找到相关房源", null);
        }
        return new ResponseEntity("200", "ok", pageBean);


    }
    @RequestMapping("/addSource")
    public ResponseEntity add ( @RequestBody AddSourceVo addSourceVo,@RequestHeader String jwt) {
Integer id = (int)jwtUtil.verifyJWTGetUserId(jwt,"id");

        sourceService.add(addSourceVo.getSource(),addSourceVo.getHouseDetail(),addSourceVo.getHousePictures(),id);
return ResponseEntity.SUCCESS;
    }
    @RequestMapping("/addUserAndSource")
    public ResponseEntity addUserAndSource(@RequestBody UserAndSource userAndSource){
        sourceService.addUserAndSource(userAndSource.getUserId(), userAndSource.getSourceId());
    return ResponseEntity.SUCCESS;
    }
    @RequestMapping("/getByUidAndSid")
    public ResponseEntity getUserAndSource(@RequestBody UserAndSource userAndSource){
      if(sourceService.getUserAndSource(userAndSource.getUserId(), userAndSource.getSourceId())){
          return new ResponseEntity<>("200","yes",null);
      } else{
           return new ResponseEntity<>("200","no",null);
      }

    }
    @RequestMapping("/deleteByUidAndSid")
    public ResponseEntity DeleteUserAndSource(@RequestBody UserAndSource userAndSource){
        sourceService.deleteByUidAndSid(userAndSource.getUserId(), userAndSource.getSourceId());
        return ResponseEntity.SUCCESS;
    }
    @RequestMapping("/getMyConcern")
    public ResponseEntity getMyConcern( Integer userId){

        List<Source> list = sourceService.getMyConcern(userId);
        return  new ResponseEntity("200","ok",list);
    }
}